package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.SwingConstants;

import java.awt.Color;

public class Modify extends JInternalFrame {
	
	Connection conn;
	DatabaseTest dt;
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JTextField modifyserial;
	private JTextField modifycode;
	private JTextField modifyea;
	private JButton btnNewButton;
	private JButton modifybtn;
	private JLabel modifystatus;
	private JButton deletebtn;


	public Modify(Connection conn, DatabaseTest dt) {
		super("데이타 수정", true, true, true, true);
		getContentPane().setBackground(new Color(255, 255, 0));
		this.conn = conn;
		this.dt = dt;
		setTitle("데이타 수정");
		setBounds(270, 0, 292, 142);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getModifyserial());
		getContentPane().add(getModifycode());
		getContentPane().add(getModifyea());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getModifybtn());
		getContentPane().add(getModifystatus());
		getContentPane().add(getDeletebtn());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("시리얼 :");
			lblNewLabel.setForeground(new Color(0, 255, 255));
			lblNewLabel.setFont(new Font("굴림", Font.BOLD, 15));
			lblNewLabel.setBounds(10, 10, 65, 20);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드 :");
			label.setForeground(new Color(0, 255, 255));
			label.setFont(new Font("굴림", Font.BOLD, 15));
			label.setBounds(10, 36, 65, 20);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량 :");
			label_1.setForeground(new Color(51, 255, 255));
			label_1.setFont(new Font("굴림", Font.BOLD, 15));
			label_1.setBounds(10, 62, 65, 20);
		}
		return label_1;
	}
	private JTextField getModifyserial() {
		if (modifyserial == null) {
			modifyserial = new JTextField();
			modifyserial.setBounds(72, 10, 116, 21);
			modifyserial.setColumns(10);
		}
		return modifyserial;
	}
	private JTextField getModifycode() {
		if (modifycode == null) {
			modifycode = new JTextField();
			modifycode.setColumns(10);
			modifycode.setBounds(72, 36, 116, 21);
		}
		return modifycode;
	}
	private JTextField getModifyea() {
		if (modifyea == null) {
			modifyea = new JTextField();
			modifyea.setColumns(10);
			modifyea.setBounds(72, 61, 116, 21);
		}
		return modifyea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.setBackground(new Color(255, 0, 0));
			btnNewButton.setFont(new Font("굴림", Font.BOLD, 15));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						if(modifyserial.getText().equals("")){
							modifystatus.setText("시리얼번호를 입력하세요.");
							modifycode.setText("");
							modifyea.setText("");
							modifybtn.setVisible(false);
						}
						else{
							int s = Integer.parseInt(modifyserial.getText());
							String sql = "select * from pro where serial = ?";
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							
							ResultSet rs = pstmt.executeQuery();
							if(rs.next()){
								modifycode.setText(rs.getString("code"));
								modifyea.setText("" + rs.getInt("ea"));
								modifystatus.setText("조회 완료!!!");
								modifyserial.requestFocus();
								modifybtn.setVisible(true);
							}
							else{
								modifystatus.setText("일치하는 데이터가 없습니다.");
								modifycode.setText("");
								modifyea.setText("");
								modifyserial.requestFocus();
								modifybtn.setVisible(false);
							}
							
							rs.close();
							pstmt.close();
						}
						
								
					}catch(Exception ex){
						ex.printStackTrace();
						modifystatus.setText("형식에 맞게 입력하세요.");
						modifycode.setText("");
						modifyea.setText("");
					}
				}
			});
			btnNewButton.setBounds(199, 9, 70, 23);
		}
		return btnNewButton;
	}
	private JButton getModifybtn() {
		if (modifybtn == null) {
			modifybtn = new JButton("수정");
			modifybtn.setVisible(false);
			modifybtn.setForeground(new Color(255, 0, 0));
			modifybtn.setBackground(new Color(0, 0, 255));
			modifybtn.setFont(new Font("굴림", Font.BOLD, 15));
			modifybtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						if(modifyserial.getText().equals("") || modifycode.getText().equals("") || modifyea.getText().equals("")){
							modifystatus.setText("수정할 값을 모두 입력하세요.");
						}
						else{
							int s = Integer.parseInt(modifyserial.getText());
							String c = modifycode.getText();
							int ea = Integer.parseInt(modifyea.getText());
							String sql = "update pro set code = ?, ea = ? where serial = ?";
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, c);
							pstmt.setInt(2, ea);
							pstmt.setInt(3, s);
							
							pstmt.executeUpdate();
							pstmt.close();
							modifystatus.setText("데이터가 수정되었습니다.");
							modifyserial.requestFocus();
						}
					}catch(Exception ex){
						ex.printStackTrace();
						modifystatus.setText("형식에 맞게 입력하세요.");
					}
					
				}
			});
			modifybtn.setBounds(200, 35, 70, 23);
		}
		return modifybtn;
	}
	private JLabel getModifystatus() {
		if (modifystatus == null) {
			modifystatus = new JLabel("");
			modifystatus.setHorizontalAlignment(SwingConstants.CENTER);
			modifystatus.setFont(new Font("굴림", Font.BOLD, 15));
			modifystatus.setBounds(10, 88, 209, 23);
		}
		return modifystatus;
	}
	private JButton getDeletebtn() {
		if (deletebtn == null) {
			deletebtn = new JButton("삭제");
			deletebtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						if(modifyserial.getText().equals("")){
							modifystatus.setText("삭제할 시리얼 번호를 입력하세요.");
						}
						else{
							int s = Integer.parseInt(modifyserial.getText());
							String sql = "delete from pro where serial = ?";
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							
							pstmt.executeUpdate();
							pstmt.close();
							modifystatus.setText("데이터가 삭제 되었습니다.");
							modifyserial.setText("");
							modifycode.setText("");
							modifyea.setText("");
							modifyserial.requestFocus();
							modifybtn.setVisible(false);
							deletebtn.setVisible(false);
						}
					}catch(Exception ex){
						ex.printStackTrace();
						modifystatus.setText("형식에 맞게 입력하세요.");
						modifybtn.setVisible(false);
					}
					
				
				
				}
			});
			deletebtn.setFont(new Font("굴림", Font.BOLD, 15));
			deletebtn.setBackground(Color.GREEN);
			deletebtn.setBounds(200, 61, 70, 23);
		}
		return deletebtn;
	}
	
	public void setData(String s, String c, String e){
		modifyserial.setText(e);
		modifycode.setText(s);
		modifyea.setText(c);
	}
}
